package com.atdfrz.secondshop.controller.user;

import com.atdfrz.secondshop.dto.CartDto;
import com.atdfrz.secondshop.dto.LayuiResult;
import com.atdfrz.secondshop.mapper.CartMapper;
import com.atdfrz.secondshop.mapper.CategoryMapper;
import com.atdfrz.secondshop.mapper.ProductMapper;
import com.atdfrz.secondshop.pojo.Cart;
import com.atdfrz.secondshop.pojo.Category;
import com.atdfrz.secondshop.pojo.Product;
import com.atdfrz.secondshop.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
public class CartController extends BaseController {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    //    //点击购物车，进入购物车页面
    @RequestMapping("/front/cart")
    public String tocart(HttpSession session, Model model) {
        //获取登录时保存的用户session对象
//        User user = (User) session.getAttribute("user2");
        Integer userid = getUidFromSession(session);
        List<CartDto> carts = cartMapper.findCartDtoByUid(userid);
        model.addAttribute("carts", carts);
        List<Category> category = categoryMapper.selectList(null);
        model.addAttribute("category",category);
        return "/front/cart";
    }

    @PostMapping("/shop-cart")
    @ResponseBody//添加商品到购物车
    public LayuiResult saveToCart(@RequestBody Cart cart, HttpSession session) {
        //获取登录时保存的用户session对象
        Integer userid = getUidFromSession(session);
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, userid);
        //根据登录的用户id获取用户的购物车信息
        List<Cart> carts = cartMapper.selectList(wrapper);
        LayuiResult<Object> result = new LayuiResult<>();
        if (carts != null) {
            for (Cart cart1 : carts) {
                if (cart1.getProductId() == cart.getProductId()) {//如果添加的商品已经在购物车中了，则将购物车中的商品数量进行增加
                    cart1.setQuantity(cart1.getQuantity() + cart.getQuantity());
                    cart1.setUpdateTime(new Date());
                    wrapper.eq(Cart::getProductId, cart1.getProductId());
                    int update = cartMapper.update(cart1, wrapper);
                    if (update != 0) {
                        result.setCode(1);
                        result.setMessage("添加成功");
                        return result;
                    } else {
                        result.setCode(0);
                        result.setMessage("添加失败");
                        return result;
                    }
                }
            }
        }
        //如果添加的商品不在购物车中
        cart.setUserId(userid);
        cart.setCreateTime(new Date());
        int insert = cartMapper.insert(cart);
        if (insert != 0) {
            result.setCode(1);
            result.setMessage("添加成功");
            return result;
        } else {
            result.setCode(0);
            result.setMessage("添加失败");
            return result;
        }
    }

    //在购物车页面中，修改购物车页面中每个商品的商品数量
    @PostMapping("/shop-cart/changeNum")
    @ResponseBody//添加商品到购物车
    public LayuiResult changeNum(@RequestBody Cart cart, HttpSession session) {
        //获取登录时保存的用户session对象
        Integer userid = getUidFromSession(session);
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, userid);
        //根据登录的用户id获取用户的购物车信息
        List<Cart> carts = cartMapper.selectList(wrapper);
        LayuiResult<Object> result = new LayuiResult<>();
        if (carts != null) {
            for (Cart cart1 : carts) {
                if (cart1.getProductId() == cart.getProductId()) {//将前端传来的购物车中商品的数量保存进数据库
                    cart1.setQuantity(cart.getQuantity());
                    cart1.setUpdateTime(new Date());
                    wrapper.eq(Cart::getProductId, cart1.getProductId());
                    int update = cartMapper.update(cart1, wrapper);
                    if (update != 0) {
                        result.setCode(1);
                        result.setMessage("添加成功");
                        return result;
                    } else {
                        result.setCode(0);
                        result.setMessage("添加失败");
                        return result;
                    }
                }
            }
//            return result;
        }
        return result;
    }

    //删除购物车中的商品
    @PostMapping("/shop-cart/deleteNum")
    @ResponseBody//添加商品到购物车
    public LayuiResult deleteNum(@RequestBody Cart cart, HttpSession session) {
        //获取登录时保存的用户session对象
        Integer userid = getUidFromSession(session);
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, userid);
        Integer productId = cart.getProductId();
        wrapper.eq(productId != null, Cart::getProductId, productId);
        int delete = cartMapper.delete(wrapper);
        LayuiResult<Object> result = new LayuiResult<>();
        if (delete != 0) {
            result.setCode(1);
            return result;
        } else {
            result.setCode(0);
            return result;
        }
    }

}
